# -*- coding=utf-8 -*-
'''
Created on Aug 10, 2016

@author: corvo
'''

from sqlalchemy import desc
from Basehandler import BaseHandler
from Utlis.ReturnCodeConfig import CodeTable
from Database.tables import Express

class QueryByCardHandler(BaseHandler):
    """
    通过一卡通号码查询之前的订单
    """

    def post(self):
        retjson = {'code':200, 'content':''}

        _card_num = ""
        try:
            # 获取参数
            _card_num = self.get_argument('card_num')
        except Exception as e:
            print (e)
            retjson['code'] = 300
            retjson['content'] = CodeTable[300]
            self.write_back(retjson)
            
        # 生成sql语句
        exist_info = self.db.query(Express)                   \
                        .filter(Express.cardnum == _card_num) \
                        .order_by(desc(Express.submittime))
        
        retarray = []
        try:
            info_array = exist_info.all()
        except Exception as e:
            print (e)
            retjson['code'] = 500
            retjson['content'] = CodeTable[500]
            self.write_back(retjson)
            return
        
        for info in info_array:
            item = {'sms': info.sms,
                    'user': info.user,
                    'phone': info.phone,
                    'dest': info.dest,
                    'arrival': info.arrival,
                    'locate': info.locate,
                    'weight': info.weight,
                    'sub_time': info.submittime.strftime("%s"),
                    'receiving': info.receiving,
                    'finish': info.finish}

            retarray.append(item)
            
        retjson['content'] = retarray
        self.write_back(retjson)
            
                
        
            
